<!-- 医师新增-编辑 -->
<template>
  <div class="vue-box submit-box--dialog" style="width: 800px">
    <div class="c-card">
      <el-form :inline="false" label-width="11em" :model="state.model" ref="$form" v-if="state.load">
        <in-input
          v-for="(item, index) in formCfg"
          :key="index"
          :type="item.type"
          :prop="item.prop"
          :name="item.label"
          :dictName="item.dictName"
          v-model="state.model[item.prop]"
          :options="item.options"
          :accessoryUrls="state.model.accessoryUrl"
          required
          @remoteMethod="remoteMethod"
        ></in-input>
      </el-form>
    </div>
  </div>
</template>
<script setup name="org-edit">
import { getCurrentInstance, onMounted, ref } from 'vue'
import {useUserStore} from "@/store/user";
const userInfo = useUserStore().userInfo
const { proxy } = getCurrentInstance()

const $form = ref(null)
// 获取插值
const prop = defineProps({
  param: { type: Object },
})
const formCfg = ref([
  {
    label: '所属医疗机构',
    prop: 'fixmedins_code',
    type: 'select',
    options: [],
  },
  {
    label: '所属科室',
    prop: 'hosp_dept_codg',
    type: 'select',
    options: [],
  },
  {
    label: '医师姓名',
    prop: 'dr_name',
  },
  {
    label: '医师编号',
    prop: 'dr_code',
  },
  {
    label: '医师类型',
    prop: 'dr_type',
    type: 'dictSelect',
    dictName: 'dr_type'
  },
  {
    label: '性别',
    prop: '',
    type: 'dictSelect',
    dictName: 'sex'
  },
  {
    label: '联系电话',
    prop: 'dr_phone',
    type: 'tel',
  },
  {
    label: '职业资格证',
    prop: 'accessoryId',
    type: 'img',
  },
])
// ------------------ 数据 ------------------
const state = reactive({
  id: proxy.param.id || 0, // 获取传值id （0=添加，非0=修改）
  model: {
    // 数据 Model
  },
  load: false,
})

// 组件加载时触发
onMounted(function () {
  if (state.id !== 0) {
    const params = {
      doctorId: state.id,
    }
    sa.ajax('/api/webPresAdmin/doctorDetail', params, function (res) {
      if (!res.data) {
        return sa.alert('数据不存在')
      }
      state.model = res.data
      state.load = true
      getOrgList()
      getDeptList()
    })
  }else {
    state.load = true
    getOrgList()
    getDeptList()
  }
})

// ------------------ 函数 ------------------
// 获取机构下拉框
function getOrgList(fixmedins_code = '',fixmedins_name = '') {
  const params = {
    pageindex: 0,
    pagesize: 50,
    fixmedins_code,
    fixmedins_name
  }
  sa.ajax('/api/webPresAdmin/medinsinfoList', params, function (res) {
    let options = res.data.map((item) => {
      return {
        value: item.fixmedins_code,
        label: item.fixmedins_name,
      }
    })
    formCfg.value.forEach(item => {
        if(item.prop == 'fixmedins_code') {
            item.options = options
        }
    })
    
  })
}
// 获取科室下拉框
function getDeptList(hosp_dept_codg = '',hosp_dept_name) {
  const params = {
    pageindex: 0,
    pagesize: 50,
    hosp_dept_codg,
    hosp_dept_name
  }
  sa.ajax('/api/webPresAdmin/deptlist', params, function (res) {
    let options = res.data.map((item) => {
      return {
        value: item.hosp_dept_codg,
        label: item.hosp_dept_name,
      }
    })
    formCfg.value.forEach(item => {
        if(item.prop == 'hosp_dept_codg') {
            item.options = options
        }
    })
    console.log('formCfg.value',formCfg.value)
  })
}

function remoteMethod(query,prop) {
    if(prop == 'fixmedins_code') {
        getOrgList('',query)
    }else if(prop == 'hosp_dept_name') {
        getDeptList('',query)
    }
}
// 提交表单
defineExpose({
  ok: async function () {
    // 表单校验
    try {
      let res = await $form.value.validate()
      console.log('res ==', res)
      let model = {
        ...state.model,
        fixmedins_code: userInfo.fixmedins_code,
        fixmedins_name: userInfo.fixmedins_name
      }
      console.log('model', model)
      // 如果是增加
      if (!state.id) {
        sa.ajax('/api/webPresAdmin/adddoctor', model, function (res) {
          sa.alert('添加成功', function () {
            sa.closeModel()
            sa.currView().f5()
          })
        })
      } else {
        // 如果是修改
        sa.ajax('/api/webPresAdmin/editdoctor', model, function (res) {
          sa.alert('修改成功', function () {
            sa.closeModel()
            sa.currView().f5()
          })
        })
      }
    } catch (error) {}
  },
})
</script>
<style></style>
